import { getCount } from '@/api/controller/cont-watch-controller/getCount'
import { ElMessage } from 'element-plus'
import { ElNotification } from 'element-plus'
import { numberFormat } from '@/utils/numberFormat.ts'
/**
 * @description 开启 Loading
 * */
export const ifWaiting = async (func) => {
  let res = await getCount()
  if (res.code === 400) {
    await func()
    const notifi = ElNotification({
      title: '处理中',
      dangerouslyUseHTMLString: true, // 使用 HTML 片段作为正文内容
      message: `<div id="kanno"><span>正在处理中</span></div>`,
      duration: 0,
      type: 'info',
    })
    const intervalId = setInterval(async () => {
      res = await getCount()
      if (res.code === 400) {
        notifi.close()
        ElNotification({
          title: '完成',
          message: '数据处理完成',
          type: 'success',
        })
        clearInterval(intervalId)
      } else {
        const kannoElement = document.getElementById('kanno')
        // 移除 span 标签
        kannoElement.querySelector('span').remove()
        // 重新插入一个新的 span 标签
        kannoElement.insertAdjacentHTML(
          'beforeend',
          `<div id="kanno"><span>${res.data.detail.message}：${numberFormat(
            res.data.detail.percent * 100,
          )}%</span></div>`,
        )
      }
    }, 3000)
  } else {
    ElMessage.warning('有数据正在处理请稍后')
  }
}
